package MonteCarloGUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

import javax.swing.JTextField;

import MultiThread.MonteCarlo;

public class MonteCarloGUI extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7371810933941988518L;
	//whole container
	private JPanel container = new JPanel();
	//text area formated w/ label
	private JTextField textAreaS = new JTextField();
	private JTextField textAreaX = new JTextField();
	private JTextField textAreaR = new JTextField();
	private JTextField textAreaT = new JTextField();
	private JTextField textAreaB = new JTextField();
	private JTextField textAreaV = new JTextField();
	private JTextField textAreaNSteps = new JTextField();
	private JTextField textAreaNSimul = new JTextField();
	private JLabel lTextAreaS = new JLabel("S : ");
	private JLabel lTextAreaX = new JLabel("X : ");
	private JLabel lTextAreaR = new JLabel("r : ");
	private JLabel lTextAreaT = new JLabel("T : ");
	private JLabel lTextAreaB = new JLabel("b : ");
	private JLabel lTextAreaV = new JLabel("v : ");
	private JLabel lTextAreaNSteps = new JLabel("nSteps : ");
	private JLabel lTextAreaNSimul = new JLabel("nSimulations : ");
	//ComboBox
	private JComboBox combo = new JComboBox();
	private JLabel labelCombo = new JLabel("Call or Put?");
	//Button to validate the whole parameters
	private JButton b = new JButton ("OK");
	//Displays results
	private JLabel lResult= new JLabel("Result: 0");
	private JLabel lTime= new JLabel("\tin 0.0 seconds");
	
	double result=0;
    
	
	
	public MonteCarloGUI() {
		//initializes the GUI
	    this.setTitle("Monte Carlo GUI: Multi Thread");
	    this.setSize(600, 200);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    this.setLocationRelativeTo(null);
	    container.setBackground(Color.white);
	    Font police = new Font("Arial", Font.ITALIC, 12);
	    //Classe for listening
	    
	    class BoutonListener implements ActionListener{
	        public void actionPerformed(ActionEvent e) {
/*		          System.out.println("S: " + textAreaS.getText());
		          System.out.println("X: " + textAreaX.getText());
		          System.out.println("r: " + textAreaR.getText());
		          System.out.println("b: " + textAreaB.getText());
		          System.out.println("T: " + textAreaT.getText());
		          System.out.println("V: " + textAreaB.getText());
		          System.out.println("nSteps: " + textAreaNSteps.getText());
		          System.out.println("nSimulations: " + textAreaNSimul.getText());
		          System.out.println("Call/Put: " + combo.getSelectedItem());*/
		          double S=Double.valueOf(textAreaS.getText());
		          double X=Double.valueOf(textAreaX.getText());
		          double R=Double.valueOf(textAreaR.getText());
		          double B=Double.valueOf(textAreaB.getText());
		          double T=Double.valueOf(textAreaT.getText());
		          double V=Double.valueOf(textAreaV.getText());
		          int Steps=Integer.valueOf(textAreaNSteps.getText());
		          int Simul=Integer.valueOf(textAreaNSimul.getText());
		          String Choice;
		          if (combo.getSelectedItem() == "Call")
		        	  Choice="c";
		          else
		        	  Choice="p";
		          MonteCarlo mtcarl = new MonteCarlo(S, X, R, T, B, V, Steps, Simul, Choice);
			  	  double start = System.nanoTime();
		          try {
					result=mtcarl.calculThread();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ExecutionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		          lResult.setText("Result: " + result);
		          double end = System.nanoTime();
		          double time=(double) ((end - start)/1.0e9);
		          lTime.setText("\tin " + time + " seconds");
		          
		          
	        }
	      }

	        
	    this.setLayout(null);
	    
/* S */
		JPanel panelS = new JPanel();
	    textAreaS.setFont(police);
	    textAreaS.setText("40");
	    textAreaS.setPreferredSize(new Dimension(60, 30));
	    panelS.add(lTextAreaS);
	    panelS.add(textAreaS);
	    container.add(panelS);
/* X */
		JPanel panelX = new JPanel();
	    textAreaX.setFont(police);
	    textAreaX.setText("50");
	    textAreaX.setPreferredSize(new Dimension(60, 30));
	    panelX.add(lTextAreaX);
	    panelX.add(textAreaX);
	    container.add(panelX);
/* r */
		JPanel panelR = new JPanel();
	    textAreaR.setFont(police);
	    textAreaR.setText("0.06");
	    textAreaR.setPreferredSize(new Dimension(60, 30));
	    panelR.add(lTextAreaR);
	    panelR.add(textAreaR);
	    container.add(panelR);
/* T */
		JPanel panelT = new JPanel();
	    textAreaT.setFont(police);
	    textAreaT.setText("0.5");
	    textAreaT.setPreferredSize(new Dimension(60, 30));
	    panelT.add(lTextAreaT);
	    panelT.add(textAreaT);
	    container.add(panelT);
/* B */
		JPanel panelB = new JPanel();
	    textAreaB.setFont(police);
	    textAreaB.setText("0.1");
	    textAreaB.setPreferredSize(new Dimension(60, 30));
	    panelB.add(lTextAreaB);
	    panelB.add(textAreaB);
	    container.add(panelB);
/* V */
		JPanel panelV = new JPanel();
	    textAreaV.setFont(police);
	    textAreaV.setText("0.45");
	    textAreaV.setPreferredSize(new Dimension(60, 30));
	    panelV.add(lTextAreaV);
	    panelV.add(textAreaV);
	    container.add(panelV);
/* nSteps */
		JPanel panelNSteps = new JPanel();
	    textAreaNSteps.setFont(police);
	    textAreaNSteps.setText("168");
	    textAreaNSteps.setPreferredSize(new Dimension(60, 30));
	    panelNSteps.add(lTextAreaNSteps);
	    panelNSteps.add(textAreaNSteps);
	    container.add(panelNSteps);
/* nSimul */
		JPanel panelNSimul = new JPanel();
	    textAreaNSimul.setFont(police);
	    textAreaNSimul.setText("100000");
	    textAreaNSimul.setPreferredSize(new Dimension(80, 30));
	    panelNSimul.add(lTextAreaNSimul);
	    panelNSimul.add(textAreaNSimul);
	    container.add(panelNSimul);
/*Put or Call*/
	    JPanel panelC = new JPanel();
	    String[] tab = {"Call", "Put"};
	    combo = new JComboBox(tab);
	    //Ajout du listener
	    combo.setPreferredSize(new Dimension(80, 30));
	    panelC.add(labelCombo);
	    panelC.add(combo);
	    container.add(panelC);
	    
	    
/* OK */
	    b.addActionListener(new BoutonListener());
	    container.add(b);
/* Results */
	    JPanel panelRes = new JPanel();
	    panelRes.add(lResult);
	    panelRes.add(lTime);
	    container.add(panelRes);
	    
	    this.setContentPane(container);
	    this.setVisible(true);
	}
}
